package article

import (
	"dog-blog/app/post/resp"
	"dog-blog/app/post/services"
	"dog-blog/base/apis"
	"strconv"

	"github.com/gin-gonic/gin"
)

type Article struct {
	apis.Api
}

// GetArticle 获取文章内容
func (e Article) GetArticle(c *gin.Context) {
	e.Context = c

	id := c.Param("id")
	article, isOk := services.FindByID(id)
	if isOk {
		e.Custom(resp.ArticleResp{BaseResp: resp.BaseResp{ErrCode: resp.OK, ErrDesc: resp.ErrDesc[resp.OK]}, Data: article})
	} else {
		e.Default(resp.ErrArticleNotFound)
	}
}

// GetArticles 获取所有文章
func (e Article) GetArticles(c *gin.Context) {
	e.Context = c

	page, pageerr := strconv.Atoi(c.Query("page"))
	limit, limiterr := strconv.Atoi(c.Query("limit"))
	category := c.Query("category")
	search := c.Query("search")

	if pageerr != nil || limiterr != nil || page < 1 {
		e.Default(resp.ErrParam)
	}

	result := resp.ArticlePageResp{BaseResp: resp.BaseResp{ErrCode: resp.OK, ErrDesc: resp.ErrDesc[resp.OK]}}
	result.Data = services.GetPage((page), limit, category, search)

	e.Custom(result)
}

// Recommend 推荐文章
func (e Article) Recommend(ctx *gin.Context) {
	articles := services.GetRecommend()
	e.Custom(resp.ArticleListResp{BaseResp: resp.BaseResp{ErrCode: resp.OK, ErrDesc: resp.ErrDesc[resp.OK]}, Data: articles})
}
